<?php

namespace common\models\c2\entity;

use cza\base\models\statics\ImageSize;
use Yii;

/**
 * This is the model class for table "{{%user_device}}".
 *
 * @property integer $id
 * @property integer $proprietor_id
 * @property integer $user_id
 * @property integer $device_id
 * @property integer $device_id2
 * @property integer $is_deleted
 * @property integer $status
 * @property string $created_at
 * @property string $updated_at
 */
class UserDevice extends \cza\base\models\ActiveRecord
{
    use \common\traits\AttachmentTrait;

    public function fields()
    {
        return [
            'id',
            'device_id',
            'device_id2',
            'cover' => function ($model) {
                // $attachment = EntityAttachment::find()
                //     ->where([
                //         'entity_id' => $model->device_id,
                //         'entity_class' => Device::className(),
                //         'entity_attribute' => 'cover',
                //     ])->one();
                $device = Device::findOne($model->device_id);
                if ($device) {
                    return $device->getInitialPreviewUrls('cover', ImageSize::ORGINAL);
                }
                return '';
            },
            'status',
            'device_name' => function ($model) {
                return $model->device->name;
            },
        ];
    }

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%user_device}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['proprietor_id', 'user_id', 'device_id', 'device_id2'], 'integer'],
            [['created_at', 'updated_at'], 'safe'],
            [['is_deleted', 'status'], 'integer', 'max' => 127],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app.c2', 'ID'),
            'proprietor_id' => Yii::t('app.c2', 'Proprietor ID'),
            'user_id' => Yii::t('app.c2', 'User ID'),
            'device_id' => Yii::t('app.c2', 'Device ID'),
            'device_id2' => Yii::t('app.c2', 'Device Id2'),
            'is_deleted' => Yii::t('app.c2', 'Is Deleted'),
            'status' => Yii::t('app.c2', 'Status'),
            'created_at' => Yii::t('app.c2', 'Created At'),
            'updated_at' => Yii::t('app.c2', 'Updated At'),
        ];
    }

    /**
     * @inheritdoc
     * @return \common\models\c2\query\UserDeviceQuery the active query used by this AR class.
     */
    public static function find()
    {
        return new \common\models\c2\query\UserDeviceQuery(get_called_class());
    }
    
    /**
    * setup default values
    **/
    public function loadDefaultValues($skipIfSet = true) {
        parent::loadDefaultValues($skipIfSet);
    }

    public function getDevice()
    {
        return $this->hasOne(Device::className(), ['id2' => 'device_id2']);
    }

    public function afterSave($insert, $changedAttributes)
    {
        parent::afterSave($insert, $changedAttributes); // TODO: Change the autogenerated stub
        $device = Device::findOne(['id2' => $this->device_id2]);
        if ($device) {
            $this->updateAttributes(['device_id' => $device->id]);
        }
    }

}
